﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Semantics;

namespace GraphBase.Presentation
{
    /// <summary>
    /// 编辑边的对话框
    /// </summary>
    public partial class EditEdgeDialog : Form
    {
        public IGraph Graph;
        IList<Node> AllNodes; //所有节点
        //List<Edge> edges;
        Node nodelbl1;
        Node nodelbl2;
        Node ntype;
        Edge editedge;
        public EditEdgeDialog(IGraph graph, Edge edge)
            :this(graph, edge.Outgoing, edge.Incoming, edge.Class)
        {
            this.editedge = edge;
        }
        public EditEdgeDialog(IGraph graph, Node outgoing, Node incoming, Node edgeclass)
        {
            this.Graph = graph;
            nodelbl1 = outgoing;
            nodelbl2 = incoming;
            ntype = edgeclass;
            InitializeComponent();
        }

        private void EditEdgeDialog_Load(object sender, EventArgs e)
        {
            AllNodes = this.Graph.GetAllNodes();

            this.listBox1.Items.Clear();
            foreach (Node node in AllNodes)
            {
                this.listBox1.Items.Add(node.Label);
            }
            if (IsHasEdge(nodelbl1, ntype, nodelbl2))
            {
                radioBtn1.Visible = true;
            }
            if (IsHasEdge(nodelbl2, ntype, nodelbl1))
            {
                radioBtn2.Visible = true;
            }
            Node1lbl.Text = nodelbl1.Label;
            Node2lbl.Text = nodelbl2.Label;
            fuzzySearchTextBox.Text = ntype.Label;
            editedge = this.Graph.GetEdge(nodelbl1, ntype, nodelbl2);
        }

        private void OKBtn_Click(object sender, EventArgs e)
        {
            
            if (radioBtn1.Checked == true)
            {
                foreach (Node node in AllNodes)
                {
                    if (node.Label == this.listBox1.SelectedItem || node.Label == fuzzySearchTextBox.Text.Trim())
                    {
                        ntype = node;
                        try
                        {
                            //this.Graph.EditEdge(editedge, nodelbl2, ntype, nodelbl1);
                            //this.GraphDatabase.ConnectNode(nodelbl2, ntype, nodelbl1);
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.ToString());
                        }
                        break;
                    }
                }
                if (MessageBox.Show("边编辑成功！", "提示", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    this.Close();
                }
            }
            else if (radioBtn2.Checked == true)
            {
                foreach (Node node in AllNodes)
                {
                    if (node.Label == this.listBox1.SelectedItem||node.Label==fuzzySearchTextBox.Text.Trim())
                    {
                        ntype = node;
                        try
                        {
                            //??this.GraphDatabase.EditEdge(editedge, nodelbl1, ntype, nodelbl2);
                            //this.GraphDatabase.ConnectNode(nodelbl1, ntype, nodelbl2);
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.ToString());
                        }
                        break;
                    }
                }
                if (MessageBox.Show("边编辑成功！", "提示", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    this.Close();
                }
            }  
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        List<Node> FuzzySearchNodes;
        private void fuzzySearchTextBox_TextChanged(object sender, EventArgs e)
       {
            this.listBox1.Items.Clear();
            string label = fuzzySearchTextBox.Text.Trim();
            if (label != "")
            {
                try
                {
                    FuzzySearchNodes = ((GraphDatabase)this.Graph).QueryNodesHasText(label);
                    foreach (Node node in FuzzySearchNodes)
                    {
                        this.listBox1.Items.Add(node.Label);
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.ToString());
                }
            }
            if (label == "")
            {
                foreach (Node node in AllNodes)
                {
                    this.listBox1.Items.Add(node.Label);
                }
            }
        }

        bool IsHasEdge(Node outgoing, Node relation, Node incoming)
        {
            Edge edge = this.Graph.GetEdge(outgoing, relation, incoming);
            if (edge != null)
                return true;
            edge = this.Graph.GetEdge(incoming, relation, outgoing);
            if (edge != null)
                return true;
            return false;
            /*
            List<Edge> edges = this.Graph.GetAllEdges;
            foreach (Edge edge in edges)
            {
                if (edge.Incoming == incoming && edge.Outgoing == outgoing && edge.FristLeaf.Class == relation)
                {
                    return true;
                }
                if (edge.Incoming == outgoing && edge.Outgoing == incoming && edge.FristLeaf.Class == relation)
                {
                    return true;
                }
            }
            return false;
             */
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.fuzzySearchTextBox.Text = listBox1.SelectedItem.ToString();
        }
    }
}
